﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Acms.Bll;

/// <summary>
/// Summary description for PageBase
/// </summary>
public class PageBase : Acms.AcmsControls.TemplatePage
{
    public PageBase()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    private  CarbonExchange.Bll.Country _UsersCountry;
    public CarbonExchange.Bll.Country UsersCountry
    {
        get
        {
            if (Request.IsAuthenticated && _UsersCountry == null)
            {
                _UsersCountry = new CarbonExchange.Bll.CountryFactory().GetById(User.Identity.User.GetCountryId());
            }
            return _UsersCountry;
        }
    }

    public string ConvertToCurrencyString(decimal footprint)
    {
        var total = ConvertToCurrency(footprint);
        if (UsersCountry != null)
        {
            return total.ToString("C", UsersCountry.CultureInfo);
        }
        else
        {
            return total.ToString("C");
        }
    }

    protected decimal ConvertToCurrency(decimal footprint)
    {
        //convert footpint to tonnes and then multiply by offsetting which is price per tonne
        var pounds = footprint * OffsettingCost.Cost * 1000;

        if (UsersCountry != null)
        {
            pounds = pounds * UsersCountry.Conversion;
        }

        return pounds;
    }

    protected override string PageTitleFormat()
    {
        return "{1}";
    }

    private CarbonExchange.Bll.OffsettingCost _offsettingCost;
    protected CarbonExchange.Bll.OffsettingCost OffsettingCost
    {
        get
        {
            if (_offsettingCost == null)
            {
                _offsettingCost = new CarbonExchange.Bll.OffsettingCostFactory().Get();
            }
            return _offsettingCost;
        }
    }
}
